package com.yhl.controller;

import com.yhl.entity.Bill;
import com.yhl.entity.Provider;
import com.yhl.mapper.ProviderMapper;
import com.yhl.service.BillService;


import com.yhl.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpSession;

    import org.springframework.stereotype.Controller;


import java.util.List;
import javax.servlet.http.HttpSession;
/**
*
* @author YaoHongli
* @since 2023-03-20 14:47:43
*/

    @Controller
public class BillController {
    @Autowired
    HttpSession session;

    @Autowired
    private BillService billService;
    @Autowired
    private ProviderService providerService;

    @RequestMapping("/billList.do")
    public String billList(String pageIndex,String queryProductName,String queryProviderId,String queryIsPayment) {
        int  currentPageNo = pageIndex==null?1:Integer.valueOf(pageIndex);
        Page page = Page.of(currentPageNo,5);
        Bill bill = new Bill();
        queryProductName=queryProductName==null?"":queryProductName;
        bill.setProductName(queryProductName);

        queryProviderId=queryProviderId==null?"0":queryProviderId;
        bill.setProviderId(Integer.parseInt(queryProviderId));

        queryIsPayment=queryIsPayment==null?"0":queryIsPayment;
        bill.setIsPayment(Integer.parseInt(queryIsPayment));

        IPage<Bill> billIPage = billService.getBillByPage(page,bill);
        List<Provider> providerList = providerService.getProviderList();
        session.setAttribute("queryProductName",queryProductName);
        session.setAttribute("queryProviderId",Integer.valueOf(queryProviderId));
        session.setAttribute("queryIsPayment",Integer.valueOf(queryIsPayment));
        session.setAttribute("providerList",providerList);
        session.setAttribute("billList",billIPage.getRecords());
        session.setAttribute("totalPageCount",billIPage.getPages());
        session.setAttribute("totalCount",billIPage.getTotal());
        session.setAttribute("currentPageNo",currentPageNo);
        return  "billlist";
    }

    @RequestMapping("/jsp/getproviderlist.do")
    @ResponseBody
    public List<Provider> providerList(){
        return providerService.getProviderList();
    }

    @RequestMapping("/addbill.do")
    public String addBill(Bill bill){

        bill.setCreationDate(LocalDateTime.now());
        billService.addBill(bill);
        return   "redirect:/billList.do";
    }

    @RequestMapping("jsp/updateBill.do")
    public String updBill(Bill bill){
             billService.updateBill(bill);
             return "redirect:/billList.do";
    }


    @RequestMapping("/jsp/deleteBill.do")
    @ResponseBody
    public HashMap delBill(String billid) {
        billService.deleteBill(billid);
        HashMap resultMap = new HashMap<>();
        resultMap.put("delResult", "true");
        return resultMap;
    }


    @RequestMapping("/jsp/viewBill.do")
    public String viewBill(String method,String billid){

        Bill bill =   billService.getBillById(billid);
        session.setAttribute("bill",bill);
        if(method.equals("view")){
            return "billview";
        }else {
            return "billmodify";
        }
    }
}